package com.example.myapplication.apter;

import android.content.Context;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.Banner;

import java.util.ArrayList;
import java.util.List;

public class ShouYeApter extends PagerAdapter {
    private List<Banner.RowsDTO> list_banner;
    private List<View> viewList;
    private Context context;


    public ShouYeApter(List<Banner.RowsDTO> list_banner, Context context) {
        this.list_banner = list_banner;
        this.context = context;

        viewList = new ArrayList<>();
        viewList.add(View.inflate(context, R.layout.layout_banner, null));
        viewList.add(View.inflate(context, R.layout.layout_banner, null));
        viewList.add(View.inflate(context, R.layout.layout_banner, null));
    }

    @Override
    public int getCount() {
        return 3;
    }


    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }



    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(viewList.get(position));
        View view = viewList.get(position);
        ImageView img = view.findViewById(R.id.img);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        if (position % 3 == 0) {
            String imgUrl = list_banner.get(position).getAdvImg();
            Glide.with(context).load(TokenBean.IP + imgUrl).into(img);
        } else if (position % 3 == 1) {
            Glide.with(context).load(TokenBean.IP+list_banner.get(position).getAdvImg()).into(img);
        } else if (position % 3 == 2) {
            Glide.with(context).load(TokenBean.IP+list_banner.get(position).getAdvImg()).into(img);
        }

        return view;
    }


    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }






}

